package encomendaFacil.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import encomendaFacil.bean.User;

public class UserDAO implements IUserDAO {
	
	private static final Logger logger = Logger.getLogger(UserDAO.class);
	
	@Override
	public User insert(User user) {
		
		Connection conn = ConnectionFactory.getInstance().getConnection();
		String sql = Queries.USER_INSERT;
		ResultSet rs = null;
		PreparedStatement ps = null;
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(0, user.getName());
			ps.setString(1, user.getLogin());
			ps.setString(2, user.getPassword());
			ps.setString(3, user.getPhone());
			
			rs = ps.executeQuery();
			if (rs.next()) {
				user.setId(rs.getLong(1));
			}
			
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		}finally{
			try {
				conn.close();
				rs.close();
				ps.close();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
			}
		}
		
		return user;
	}

	@Override
	public void remove(Long id) {
		
		Connection conn = ConnectionFactory.getInstance().getConnection();
		String sql = Queries.USER_REMOVE;
		PreparedStatement ps = null;
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setLong(0, id);
			ps.executeQuery();
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		} finally{
			try {
				conn.close();
				ps.close();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
			}
		}
	}

	@Override
	public User findById(Long id) {
		
		Connection conn = ConnectionFactory.getInstance().getConnection();
		String sql = Queries.USER_FIND_BY_ID;
		ResultSet rs = null;
		User user = null;
		PreparedStatement ps = null;
		
		try {
			
			ps = conn.prepareStatement(sql);
			ps.setLong(0, id);
			rs = ps.executeQuery();
			
			if (rs.next()) {
				user = new User();
				user.setId(id);
				user.setLogin(rs.getString(ColumnIndexes.LOGIN));
				user.setName(rs.getString(ColumnIndexes.USER_NAME));
				user.setPassword(rs.getString(ColumnIndexes.PASSWORD));
				user.setPhone(rs.getString(ColumnIndexes.USER_PHONE));
			}
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		}finally{
			try {
				conn.close();
				rs.close();
				ps.close();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
			}
		}
		
		return user;
	}

	public boolean alterPassword(String password, Long id){
		Connection conn = ConnectionFactory.getInstance().getConnection();
		String sql = Queries.USER_ALTER_PASSWORD;
		int columnsAffected = 0;
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(0, password);
			ps.setLong(1, id);
			
			columnsAffected = ps.executeUpdate();
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		} finally{
			try {
				conn.close();
				ps.close();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
			}
			
		}
		return columnsAffected == 1 ? true : false;
	}
	
	public User login(String login, String password){
		Connection conn = ConnectionFactory.getInstance().getConnection();
		String sql = Queries.USER_LOGIN;
		ResultSet rs = null;
		User user = null;
		PreparedStatement ps = null;
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(0, login);
			ps.setString(1, password);
			rs = ps.executeQuery();
			
			if (rs.next()) {
				user = new User();
				user.setId(rs.getLong(ColumnIndexes.USER_ID));
				user.setName(rs.getString(ColumnIndexes.USER_NAME));
				user.setLogin(rs.getString(ColumnIndexes.LOGIN));
				user.setPassword(rs.getString(ColumnIndexes.PASSWORD));
				user.setPhone(rs.getString(ColumnIndexes.USER_PHONE));
			}
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		} finally{
			try {
				conn.close();
				rs.close();
				ps.close();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
			}
		}
		
		return user;
	}

}
